﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZerAx.Net
{
    public class RingMemoryStream : Stream
    {
        private RingBuffer raw;

        public RingMemoryStream()
        {
            raw = new RingBuffer(128);
        }
        
        public RingMemoryStream(int capacity)
        {
            raw = new RingBuffer(capacity);
        }

        public byte[] GetBuffer() => raw.GetBuffer();

        public override bool CanRead => raw.Length > 0;

        public override bool CanSeek => false;

        public override bool CanWrite => true;

        public override long Length => raw.Capacity;

        public override long Position { get; set; }

        public override void Flush() { }
        public override int Read(byte[] buffer, int offset, int count)
        {
            return raw.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return 0;
        }

        public override void SetLength(long value)
        {
            raw.Expand((int)value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            raw.Write(buffer, offset, count);
        }
    }
}
